package com.assess.web.service.impl;

import com.assess.web.dao.PhasesMapper;
import com.assess.web.model.Phases;
import com.assess.web.model.PhasesExample;
import com.assess.web.service.PhasesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PhasesServiceImpl implements PhasesService {

    @Autowired
    private PhasesMapper phasesMapper;


    @Override
    public List<Phases> list(Integer parentId) {
        if (null == parentId) {
            parentId = 0;
        }
        PhasesExample example = new PhasesExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        return phasesMapper.selectByExample(example);
    }

    @Override
    public int save(Integer parentId, String name) {
        Phases phases = new Phases();
        phases.setName(name);
        if (null != parentId) {
            phases.setParentId(parentId);
        }
        return phasesMapper.insertSelective(phases);
    }

    @Override
    public void delete(int id) {
        PhasesExample example = new PhasesExample();
        example.or().andParentIdEqualTo(id);
        example.or().andIdEqualTo(id);
        phasesMapper.deleteByExample(example);
    }
}
